#!/usr/bin/perl
use warnings;
use strict;

#比对计数；small RNA在基因区域和外显子区域分布数目；
@files = <ago4-2-*.bwt>;
die ("wrong input!\n") unless(@files == 3);
foreach $file(@files){
    my %hash_type;
	#my $i;
	open STDIN,"<","$file" or die "can't open file $file!\n";
	while (<STDIN>){
		chomp;
		@row = split(/\s+/);
		#$strand = $row[1];				#+/-;
		$id = $row[0];
		$chromosome = $row[2];			#chloroplast/mitochondria;
		if ($chromosome eq chloroplast){
			$chromosome = "ChrC";
		}elsif($chromosome eq mitochondria){
			$chromosome = "ChrM";
		}else{
			$chromosome = $chromosome;
		}
		$start = $row[3];				#起始位置;
		$seq = $row[4];					#Sequence;
		$end = $start + length($seq)-1;	#终止位置；
		open IN,"<","gene_exon.gff.txt" or die "can't open file gene_exon.gff.txt!\n";
		while (<IN>){
			chomp;
			@line = split(/\s+/);
			$chr = $line[0];
			$type = $line[1];
			$begin = $line[2];
			$final = $line[3];
			if (lc($chromosome) eq lc($chr) && $start >= $begin && $end <= $final){
				#if ($type eq exon ||$type eq )
				#@location = ();
				#push @location,$id;
				#push @location,$type;
				#push @location,$chr;
				#push @location,$start;
				#push @location,$end;
				#push @location,$begin;
				#push @location,$final;
				#open STDOUT,">>","location_$file.txt" or die "can't creat file location_$file.txt!\n";
				#print "@location\n";
				#close STDOUT;
				$hash_type{$type}++;
				last;
			#}elsif (lc(chromosome) eq lc($chr) && (($start<$begin && $begin<$end) ||($start<$final && $final<$end))){
			#	$i++;
			}else{
				next;
			}
		}	
	}
	close IN;
	close STDIN;
	open STDOUT,">>","type_$file.txt" or die "can't creat file type_$file.txt!\n";	
	while (($key,$value)=each %hash_type){
		print "$key => $value\n";
	}
	#print $i;
	close STDOUT;
}
